package com.yunchen.BooksManagementSystem.login.service;

import com.yunchen.BooksManagementSystem.login.dao.LoginDao;
import com.yunchen.BooksManagementSystem.login.dao.LoginDaoImpl;
import com.yunchen.BooksManagementSystem.utils.DBUtil;

import java.sql.*;

/**
 * @创建人
 * @创建时间
 * @描述
 */
public class LoginServiceImpl implements LoginService {
    DBUtil dbUtil = new DBUtil();
    LoginDao loginDao = new LoginDaoImpl();
    Connection conn = DBUtil.getConnection();
    Statement stmt;

    {
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private String userId1 = null;
    private String password1 = null;

    @Override
    public boolean isLogin(String id, String password) {
        String sql = "SELECT * FROM userinfo WHERE userId = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                userId1 = rs.getString("userid");
                password1 = rs.getString("password");
                if (id.equals(userId1) && password.equals(password1)) {
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }
    }

    /**
     * @param name
     * @param grade
     * @param direction
     * @return
     * 查找validationInfo（验证信息）中是否存在该学员
     */
    public boolean isRegister1(String name, String grade, String direction) {
        String sql = "SELECT * FROM validationinfo WHERE name = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String name1 = rs.getString("name");
                String grade1 = rs.getString("grade");
                String direction1 = rs.getString("direction");
                if (name.equals(name1) && grade.equals(grade1) && direction.equals(direction1)) {
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param userId
     * @return
     * 查找userInfo（用户信息）中该id是否已被注册
     */
    @Override
    public boolean isRegister2(String userId) {
        String sql = "SELECT userId FROM userinfo";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String userId1 = rs.getString("userId");
                if (userId.equals(userId1)){
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param invitation
     * @return
     * 查找invitation（邀请码）中是否存在该邀请码
     */
    @Override
    public boolean isRegister3(String invitation) {
        String sql = "SELECT * FROM invitation WHERE code = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,invitation);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String invitation1 = rs.getString("code");
                if (invitation.equals(invitation1)){
                    return true;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    /**
     *
     * @param name
     * @return
     * 查找userInfo（用户信息）中该学员是否已经注册
     */
    public boolean isRegister4(String name){
        String sql = "SELECT name FROM userinfo";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String name1 = rs.getString("name");
                if (name.equals(name1)){
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isExist(String id) {
        String sql = "SELECT * FROM userinfo WHERE userId = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                userId1 = rs.getString("userid");
                if (id.equals(userId1)) {
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }
    }

    public boolean isAdm(String id) {
        String sql = "SELECT * FROM userinfo WHERE userId = '@123456'";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                userId1 = rs.getString("userid");
                if (id.equals(userId1)) {
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isInvite(String invitation) {
        String sql = "SELECT * FROM invitation WHERE code = ?";
        try {
            PreparedStatement ps1 = conn.prepareStatement(sql);
            ResultSet rs1 = ps1.executeQuery();
            while (rs1.next()) {
                String invitation1 = rs1.getString("code");
                if (invitation.equals(invitation1)) {
                    return true;
                }
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public void register(String name, String grade, String direction, String id, String password) throws SQLException {
        loginDao.addUser(stmt, id, password, name, grade, direction);
    }

}